<?php

namespace Models;

class Cashier extends Model
{
	protected $_table = 'cashiers';

	protected $_filter = false;

	// 登录事件
	public function login()
	{
		$this->db->insert('cashier_login_logs', [
			'cashier_id' => $this['id'],
			'login_time' => date('Y-m-d H:i:s'),
		]);
	}

	// 退出事件
	public function logout()
	{
		$log = $this->db->where([
				'status' => 0,
				'cashier_id' => $this['id'],
				'login_time >=' =>  date('Y-m-d'),
			])
			->order_by('id desc')
			->get('cashier_login_logs')->row_array();
		if ($log) {
			$alive_time = round((strtotime(date('Y-m-d H:i:s')) - strtotime($log['login_time']))/3600, 2);
			$this->db->where('id', $log['id'])->update('cashier_login_logs', [
				'status' => 1,
				'logout_time' => date('Y-m-d H:i:s'),
				'alive_time' => $alive_time,
			]);
		} else {
			$this->db->insert('cashier_login_logs', [
				'cashier_id' => $this['id'],
				'logout_time' => date('Y-m-d H:i:s'),
			]);
		}
	}

	//获取上次登录
	public function last_login(){
		$log = $this->db->where([
				'status' => 0,
				'cashier_id' => $this['id'],
				'login_time >=' =>  date('Y-m-d'),
			])
			->order_by('id desc')
			->get('cashier_login_logs')->row_array();
		return $log;
	}
}